package com.zhz.lib.project.controller;

import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.zhz.lib.project.controller.form.NewsForm;
import com.zhz.lib.project.ibatis.dataobject.News;
import com.zhz.lib.project.service.custom.CustomService;
import com.zhz.lib.project.service.news.NewsService;
import com.zhz.lib.project.vo.custom.CustomVO;
import com.zhz.lib.project.vo.news.NewsVO;
import com.zhz.project.common.util.alibaba.StringUtil;
import com.zhz.project.common.util.ibatispage.IbatisPage;
import com.zhz.project.common.util.zhzutil.ZhzBeanUtils;
import com.zhz.project.common.util.zhzutil.ZhzHttpRequestUtil;

/**
 * @author LiWei <a href="mailto:liwei2672@gmail.com">liwei2672@gmail.com</a>
 * @version 创建时间：2010-5-3 下午03:22:27
 * 
 */
@Controller
@RequestMapping("/news.htm")
public class NewsController {
	private static Logger logger = Logger.getLogger(NewsController.class);
	private static int firstResult = 0;
	private static int maxResults = 7;
	@Autowired
	private NewsService newsService;
	@Autowired
	private IbatisPage ibatisPage;
	@Autowired
	private CustomService customService;

	private static String pageSql = "selectAllNewsByPage";
	private static String countSql = "selectAllNewsCount";

	/**
	 * 最新消息列表(Ibatis)
	 * 
	 * @param model
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(method = RequestMethod.POST, params = "action=page")
	public String getIbatisListPost(ModelMap model, NewsForm newsForm) {
		return page(model, newsForm);
	}

	/**
	 * 最新消息列表(Ibatis)
	 * 
	 * @param model
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(method = RequestMethod.GET, params = "action=page")
	public String getIbatisListGet(ModelMap model, NewsForm newsForm) {
		return page(model, newsForm);
	}

	@SuppressWarnings("unchecked")
	private String page(ModelMap model, NewsForm newsForm) {
		ZhzHttpRequestUtil.writeMapToModelMap(model, ibatisPage.getPage(
				ZhzBeanUtils.describe(newsForm), countSql, pageSql));

		// 将News转换为NewsVO
		List<News> newsList = (List<News>) model.get("list");
		List<NewsVO> newsVOList = new ArrayList<NewsVO>();
		if (newsList != null && newsList.size() > 0) {
			for (News news : newsList) {
				NewsVO newsVO = new NewsVO();
				BeanUtils.copyProperties(news, newsVO);
				newsVOList.add(newsVO);
			}
		}

		model.put("list", newsVOList);

		// 获取置顶最新消息
		model.put("topNewsVOList", newsService.getTopNews(firstResult,
				maxResults));

		// 获取最新消息类型
		model.put("typeList", newsService.getNewsType());

		// 获取顶级模块以及对应的子模块
		model.put("customVOList", customService.getTopCustom());

		return "news";
	}

	/**
	 * 
	 * 根据id获取最新消息
	 * 
	 * @param model
	 * @param request
	 * @param response
	 * @param id
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(method = RequestMethod.GET, params = "event=single")
	public String getSingleNews(ModelMap model, HttpServletRequest request,
			HttpServletResponse response, @RequestParam("id") int id) {

		// 获取顶级模块以及对应的子模块
		model.put("customVOList", customService.getTopCustom());

		model.put("news", newsService.getNewsById(id));
		newsService.newsHitPlusOne(id);
		return "newsDetail";
	}

}
